#include <ev.h>
#include <amqpcpp.h>
#include <amqpcpp/libev.h>
#include <openssl/ssl.h>
#include <openssl/opensslv.h>

int main()
{
    //实例化底层网络通信库的句柄
    auto* loop = EV_DEFAULT;
    //实例化libEvHandle句柄，将AMQP框架和事件监控关联起来
    AMQP::LibEvHandler handler(loop);
    //实例化连接对象
    AMQP::Address address("amqp://root:123456@127.0.0.1:5672/");
    AMQP::TcpConnection connection(&handler,address);

    //创建通道对象
    AMQP::TcpChannel channel(&connection);
    //声明交换机
    channel.declareExchange("test-exchange",AMQP::ExchangeType::direct)
    .onError([](const char* message){
        std::cout << "声明交换机失败\n";
        return -1;
    })
    .onSuccess([](){
        std::cout << "声明交换机成功\n";
    });

    //声明队列
    channel.declareQueue("test-queue")
    .onError([](const char* message){
        std::cout << "声明队列失败\n";
        return -1; 
    })
    .onSuccess([](){
        std::cout << "声明队列成功\n";
    });

    channel.bindQueue("test-exchange", "test-queue", "test-queue-key")
        .onError([](const char *message)
                 {
            std::cout << "test-exchange - test-queue 绑定失败：" << message << std::endl;
            exit(0); })
        .onSuccess([]()
                   { std::cout << "test-exchange - test-queue 绑定成功！" << std::endl; });

    //发送消息
    for(int i =0; i<10; i++) {
        std::string msg = "hello 这是一条测试消息" + std::to_string(i);
        bool ret = channel.publish("test-exchange","test-queue-key",msg);
        if(ret == false) {
            std::cout << "publish 失败\n";
        }
    }

    //启动网络库,开始IO事件监听
    ev_run(loop,0);

    return 0;
}